package com.matrix.rabbitmqtest.deadqueue;

import cn.hutool.core.lang.Console;
import com.matrix.rabbitmqtest.util.AppKeys;
import com.matrix.rabbitmqtest.util.RabbitMQUtil;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 死信队列
 * 死信队列专属消费者
 */
public class DeadLetterConsumer {
    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtil.getChannel();
        channel.queueDeclare(AppKeys.DEAD_QUEUE, false, false, false, null);
        // 开始消费死信
        Console.log("开始等待死信以消费...");
        channel.basicConsume(AppKeys.DEAD_QUEUE,true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                Console.log("接收到死信: {}",new String(body, StandardCharsets.UTF_8));
            }
        });
    }
}
